import { CreateDist, DistStruct } from "../../common/src/CreateDist";
import { LoadComponent } from "../../common/src/LoadComponent";
import { Component, } from "../../../component";
import { RunComponent } from "../../common/src/RunComponent";
import { ComponentInfo } from "../../common/src/ComponentInfo";
import { idGen } from "./utils/IdGenerator";
import { ImplementComponent } from "./ImplementComponent";
import { LoadDev } from "./scaffold/AddLayui";
import { Beautify } from "./utils/Beautify";
import { Configurable } from "./scaffold/Configurable";

export type ServerComponentType = Component<
  cheerio.Root,
  cheerio.Cheerio
>;

export class Main {
  private distStructure: DistStruct;
  private projectRoot: string;
  constructor(projectRoot: string, clean: boolean) {
    this.projectRoot = projectRoot;
    this.distStructure = new CreateDist(
      this.projectRoot,
      "dist",
      clean
    ).getDistStructure();
  }

  public loadComponent(name: string) {
    const ci = new ComponentInfo(name, this.projectRoot);
    const lc = new LoadComponent(ci);
    const module = lc.loadComponent();

    return {
      module,
      componentInfo: ci,
    };
  }
  public runComponent(ci: ComponentInfo, module: ServerComponentType) {
    // const componentId = IdGenerator.gen();
    const componentId = idGen.gen()

    const runtime = new RunComponent(
      ci,
      module,
      componentId,
      this.distStructure
    );

    const runtimeModule = runtime.getModule();
    runtime.handleResources(
      runtimeModule.require ? runtimeModule.require() : {}
    );

    return runtime;
  }
  public implementComponent(runtime: RunComponent) {
    const impl = new ImplementComponent(runtime, this.distStructure);
    impl.write();

    return impl;
  }
  public dev() {
    new LoadDev(this.distStructure);
  }
  public setConfigs(
    com: ServerComponentType,
    im: ImplementComponent,
    runtime: RunComponent
  ) {
    return new Configurable(com, im, runtime, this.distStructure);
  }
  public beautify() {
    return new Beautify(this.distStructure);
  }
  // public sync() {
  //   this.syncInstance.sync();
  // }
}

// new Main();
